package yzserver.sx4a.job;

import yzserver.sx4a.RetTools;
import yzserver.sx4a.dao.IJobDao;
import yzserver.sx4a.domain.JobDomain;
import yzserver.sx4a.form.Job;
import yzserver.sx4a.form.Ret;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: andrew
 * Date: 14-8-15
 * Time: 上午10:14
 * To change this template use File | Settings | File Templates.
 */
public class Sx4aJobImpl implements Sx4aJob {

    private IJobDao jobDao;

    public void setJobDao(IJobDao jobDao) {
        this.jobDao = jobDao;
    }

    @Override
    public Job selJob(String jobid) {
        JobDomain domain = jobDao.selJob(jobid);
        if(domain!=null){
            Job job = new Job(domain);
            return job;
        }else{
            return null;
        }

    }

    @Override
    public Ret addJob(Job job) {
        try {
            jobDao.addJob(new JobDomain(job));
        } catch (Exception e) {
            return RetTools.getRet("创建职务失败", e);
        }
        return RetTools.getRet("创建职务成功", null);
    }

    @Override
    public Ret updJob(Job job) {
        try {
            jobDao.updJob(new JobDomain(job));
        } catch (Exception e) {
            return RetTools.getRet("修改职务失败", e);
        }
        return RetTools.getRet("修改职务成功", null);
    }

    @Override
    public Ret delJob(String jobid) {
        try {
            jobDao.delJob(jobid);
        } catch (Exception e) {
            return RetTools.getRet("删除职务失败", e);
        }
        return RetTools.getRet("删除职务成功", null);

    }

    @Override
    public List selAllJob(String jobid) {
        List<JobDomain> jobDomains = new ArrayList<JobDomain>();
        List  jobList = new ArrayList();
        if("all".equalsIgnoreCase(jobid)){ // all为接口约定的固定值
            jobDomains = jobDao.selAllJob(jobid);
            if(jobDomains!=null){
                for (int i = 0; i <jobDomains.size(); i++) {
                    Job job = new Job(jobDomains.get(i));
                    jobList.add(job);
                }
            }
        }
        return jobList;
    }
}
